import logger from '../../core/logger';
import apiProvider from './apiProvider';
import current from '../../core/current';
import apiKeyInfo from '../../model/common/apiKeyInfo';

export default class dataInfo {
  private _conditions: string;

  public get conditions() {
    return this._conditions;
  }

  public set conditions(condition: string) {
    this._conditions = condition;
  }

  private _buildVer: string = apiProvider.Build5;

  public get buildVer() {
    return this._buildVer;
  }

  public set buildVer(buildVer: string) {
    this._buildVer = buildVer;
  }

  constructor(condition: string, buildVer?: string) {
    this._conditions = condition;
    if (buildVer)
      this._buildVer = buildVer;
  }

  public toString(keyInfo: apiKeyInfo = apiProvider.AndroidKey): string {
    let builder = '';
    try {
      if (this._conditions != '') {
        if (this._conditions.startsWith('&'))
          this._conditions = this._conditions.substring(1, this._conditions.length - 1);
        builder = this._conditions + '&';
      }
      if (keyInfo == apiProvider.LoginKey) {
        builder += `&access_key=&appkey=${keyInfo.appkey}&platform=android&mobi_app=android&device=phone&ts=${apiProvider.timeSpanSeconds}&build=${apiProvider.Build7}`;
      } else {
        if (current.instance.isLogin)
          builder += `access_key=${current.instance.accesskey}&`;
        builder += "appkey=" + keyInfo.appkey + "&build=" + this._buildVer + "&platform=android&mobi_app=android";
        builder += "&ts=" + apiProvider.timeSpanSeconds;
      }
      builder += "&sign=" + apiProvider.getSign(builder, keyInfo);
    } catch (e) {
      logger.error(e);
    }
    return builder;
  }
}